package com.jingan.nursinghome.regulatory.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.base.BaseView;
import cn.android.support.v7.lib.sin.crown.utils.AssetsUtils;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/11/4.
 */

public class BitmapsView extends BaseView {
    public BitmapsView(Context context) {
        super(context);
        init();
    }

    public BitmapsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    int widht = ProportionUtils.getInstance().adapterInt(110);
    int height = (int) (widht * 1.777);
    int maxHeiht = height;
    int offset = widht / 10;

    Bitmap cancel = AssetsUtils.getInstance().getBitmapFromAssets( "图标/取消.png", 0, true);

    private void init() {
        cancel = ProportionUtils.getInstance().adapterBitmap(cancel);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension((widht + offset * 2) * bitmaps.size(), (height + offset * 2) * bitmaps.size());
    }

    public List<Bitmap> bitmaps = new ArrayList<>();

    public void setBitmaps(List<Bitmap> bitmaps) {
        this.bitmaps.clear();
        //Log.e("test", "宽:\t" + bitmaps.get(0).getWidth() + "\t高:\t" + bitmaps.get(0).getHeight());
        for (int i = 0; i < bitmaps.size(); i++) {

            float f = 1;
            if (bitmaps.get(i).getHeight() > bitmaps.get(i).getWidth()) {
                f = bitmaps.get(i).getHeight() / bitmaps.get(i).getWidth();
            } else {
                f = bitmaps.get(i).getWidth() / bitmaps.get(i).getHeight();
            }
            height = (int) (f * widht);

            maxHeiht = maxHeiht > height ? maxHeiht : height;

            this.bitmaps.add(Bitmap.createScaledBitmap(bitmaps.get(i), widht, height, true));
        }
        requestLayout();
    }

    List<Region> regions = new ArrayList<>();

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        regions.clear();
        for (int i = 0; i < bitmaps.size(); i++) {
            int x = offset / 2 + (widht + offset) * i;
            canvas.drawBitmap(bitmaps.get(i), x + offset / 2, offset, paint);
            canvas.drawBitmap(cancel, x, 0, paint);

            //区域范围判断，如检测一个点是否在该区域
            Region region = new Region();
            Path path = new Path();
            path.addRect(x, 0, x + cancel.getWidth(), cancel.getHeight(), Path.Direction.CCW);
            RectF rectF = new RectF();
            //计算路径的区域，将路径的左上右下四个顶点转换成RectF
            path.computeBounds(rectF, true);
            Rect rect = new Rect();
            rect.set((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom);//自定义区域，只对该区域的路径有效果。即该区域的点返回true，其他区域为false
            //参数一 路径，参数二 路径所在区域。只对该区域的路径进行检测
            region.setPath(path, new Region(rect.left, rect.top, rect.right, rect.bottom));
            regions.add(region);
        }
    }

    BaseListenner<Integer> baseListener;

    public void setBaseListener(BaseListenner<Integer> baseListener) {
        this.baseListener = baseListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (baseListener != null) {
            for (int i = 0; i < regions.size(); i++) {
                if (regions.get(i).contains((int) event.getX(), (int) event.getY())) {
                    baseListener.onResult(i);
                }
            }
        }
        return super.onTouchEvent(event);
    }
}
